package com.hiinfo.mvpdemo.weather;

import android.util.Log;

import com.hiinfo.mvpdemo.base.bean.BaseObjectBean;
import com.hiinfo.mvpdemo.base.component.BasePresenter;
import com.hiinfo.mvpdemo.net.RxScheduler;

import io.reactivex.functions.Consumer;

/**
 * @author : lifei
 * 创建时间： 19-6-28
 * 更改时间： 19-6-28
 * Email : allen.lee723@qq.com
 * @version :
 * Description：
 */
public class WeatherPresent extends BasePresenter<WeatherContract.View> implements WeatherContract.Presenter {

    private WeatherContract.Model model;

    public WeatherPresent() {
        model = new WeatherModel();
    }

    @Override
    public void queryWeather() {
        // View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        mView.startLoading();
        mView.showLoading();

        model.queryWeather()
            .compose(RxScheduler.<BaseObjectBean<WeatherInfo>>Flo_io_main())
            .as(mView.<BaseObjectBean<WeatherInfo>>bindAutoDispose())
            .subscribe(new Consumer<BaseObjectBean<WeatherInfo>>() {
                @Override
                public void accept(BaseObjectBean<WeatherInfo> bean) throws Exception {
                    if (bean.getWeatherinfo() == null) {
                        Log.e("LoginPresenter", "accept: null");
                    }
                    mView.onSuccess(bean);
                    mView.hideLoading();
                    mView.endLoading();
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    mView.onError(throwable);
                    mView.hideLoading();
                }
            });
    }
}